package com.dongnan.hl7v2;

import java.io.ObjectStreamException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class MyParaMap implements Serializable {

    private static final long serialVersionUID = -4827759752671402638L;
    private volatile static MyParaMap myParaMap = null;
    private DataBaseCconnectPool pool = DataBaseCconnectPool.getInstance();

    public List<MessageTypeAndTableMap> messageTypeAndTableMapList = new ArrayList<>();
    public List<String> sqlList = new ArrayList<>();

    private MyParaMap() throws SQLException {

        String sql = "SELECT * FROM `messageType_tableName_map`";//SQL语句
        Connection conn = pool.getConnection();
        Statement st = conn.createStatement();
        ResultSet resultSet = null;
        resultSet = st.executeQuery(sql);
        while(resultSet.next())
        {
            String messageType=resultSet.getString("message_type");
            String conditionHl7FieldName = resultSet.getString("condition_hl7Field_name");
            String conditionHl7FieldValue = resultSet.getString("condition_hl7Field_value");
            String tableTame = resultSet.getString("table_name");
            MessageTypeAndTableMap messageTypeAndTableMap = new MessageTypeAndTableMap(messageType);

            TableNameAndFieldMap tableNameAndFieldMap = new TableNameAndFieldMap(tableTame,conditionHl7FieldName,conditionHl7FieldValue);
            String tempSql = "SELECT * FROM `tableName_hl7Field_map` WHERE `table_name` = '"+tableTame+"'";

            Connection tmpConn = pool.getConnection();
            Statement tmpSt = tmpConn.createStatement();
            ResultSet tmpResultSet = null;
            tmpResultSet = tmpSt.executeQuery(tempSql);
            int count = 0;
            while(tmpResultSet.next())
            {
                String tableFieldName = tmpResultSet.getString("table_field_name");
                String hl7FieldName = tmpResultSet.getString("hl7_field_name");
                tableNameAndFieldMap.add(tableFieldName,hl7FieldName);
                count++;
            }
            if(count == 0){
                System.out.println("消息【"+messageType +"】 配置了对应的表 【" + tableTame + "】，但未添加字段映射");
            }
            tableNameAndFieldMap.add("TABLE_NAME_DISTINCT",conditionHl7FieldName);
            messageTypeAndTableMap.tableNameAndFieldMap = tableNameAndFieldMap;
            messageTypeAndTableMapList.add(messageTypeAndTableMap);
            tmpConn.close();
            tmpSt.close();
            tmpResultSet.close();
        }
        conn.close();
        st.close();
        resultSet.close();

    }

    public static MyParaMap getInstance(){
        if(myParaMap == null){
            synchronized(MyParaMap.class){
                if(myParaMap == null){
                    try {
                        myParaMap = new MyParaMap();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return myParaMap;
    }

    @Override
    public String toString() {
        return "映射配置初始化";
    }

    private Object readResolve() throws ObjectStreamException {
        return getInstance();
    }

    public void getSQLList(String messageType) throws Exception {
        if(null == sqlList){
            sqlList = new ArrayList<>();
        }
        for(MessageTypeAndTableMap messageTypeAndTableMap:messageTypeAndTableMapList){
            if(messageType.equals(messageTypeAndTableMap.messageTypeName)) {
                sqlList.addAll(messageTypeAndTableMap.tableNameAndFieldMap.getSqlList());
            }
        }
    }

    public void cleanResult() {
        for(MessageTypeAndTableMap messageTypeAndTableMap:messageTypeAndTableMapList){
            messageTypeAndTableMap.tableNameAndFieldMap.cleanResult();
        }
        sqlList = new ArrayList<>();
    }

}
